﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
/// News 新闻公告类
/// </summary>
public class News
{
    private  DBOperation _db;
    private string _newsTitle;
    private string _newsContent;
    private string _addTime;
    public string NewsTitle
    {
        set { _newsTitle = value; }
        get { return _newsTitle; }
    }
    public string NewsContent
    {
        set { _newsContent = value; }
        get { return _newsContent; }
    }
    public string AddTime
    {
        set { _addTime = value; }
        get { return _addTime; }
    }
	public News()
	{
        _db = new DBOperation();
		//
		// TODO: 在此处添加构造函数逻辑
		//
	}
    public News(string title,string content,string addTime)
    {
        _newsTitle = title;
        _newsContent = content;
        _addTime = addTime;
    }
    /// <summary>
    /// 添加新闻
    /// </summary>
    /// <returns>成功返回真，否则假</returns>
    public bool AddNews()
    {
        bool seccess = false;
        string strAddNews = "insert into NEWS(newsTitle,newsContent,addTime) values("
                             +"'"+this.NewsTitle+"',"
                             +"'"+this.NewsContent+"',"
                             +"'"+this.AddTime+"')";
        seccess = _db.ExcuteSql(strAddNews);
        return seccess;
    }
    /// <summary>
    /// 删除新闻
    /// </summary>
    /// <param name="newsID">新闻ID</param>
    /// <returns>成功返回真、否则 假</returns>
    public bool DeleteNews(int newsID)
    {
        bool seccess = false;
        string strDeleteNews = "delete from NEWS where newsID=" + newsID ;
        seccess = _db.ExcuteSql(strDeleteNews);
        return seccess;
 
    }
    /// <summary>
    /// 读取系统中最新前15条新闻
    /// </summary>
    /// <returns>dataSet</returns>
    public DataSet ReadAllNews()
    {
        DataSet ds = null;
        string strReadAllNews = "select top 20 * from NEWS order by newsID desc";
        string tableName = "NEWS";
        ds = _db.GetDataSet(strReadAllNews,tableName);
        return ds;
    }
    /// <summary>
    /// 根据新闻ID读取单条新闻
    /// </summary>
    /// <param name="newsID">新闻ID</param>
    /// <returns>返回一个datasetr</returns>
    public DataSet  GetSingleNews(int newsID)
    {
        DataSet  ds = null;
        string strGetSingleNews = "select * from NEWS where newsID="+newsID;
        string tableName = "news";
        ds = _db.GetDataSet(strGetSingleNews,tableName);
        return ds;
    }
    /// <summary>
    /// 同时删除多条新闻
    /// </summary>
    /// <param name="newsID">需要删除的新闻ID</param>
    /// <returns>成功返回真、否则假</returns>
    public bool DeleteNewses(ArrayList sqllist)
    {

        bool seccess = false;
        seccess = _db.ExcuteSqls(sqllist);
        return seccess;

    }
    /// <summary>
    /// 读取系统中的所有新闻
    /// </summary>
    /// <returns></returns>
    public DataSet ReadRecordNews()
    {
        DataSet ds = null;
        string strReadAllNews = "select  * from NEWS order by newsID desc";
        string tableName = "NEWS";
        ds = _db.GetDataSet(strReadAllNews, tableName);
        return ds;
    }
    /// <summary>
    /// 自动删除新闻，每月一号新闻计数，保留最新的30条新闻
    /// </summary>
    public void AutoDelNews()
    {
        int  day = DateTime.Now.Day;
        if(day==1)
        {
            
            string strReadNews = "select * from NEWS";
            String tableName = "news";
            DataSet ds = _db.GetDataSet(strReadNews,tableName);
            int count = ds.Tables["news"].Rows.Count;
            if(count>30)
            {
                ArrayList sqlList = new ArrayList();
                int lastID =Convert.ToInt32( ds.Tables["news"].Rows[count - 1]["newsID"]);
                int firstID = lastID - count+1;
                int startID = lastID - 29;
                for (int i = firstID; i < startID;i++ )
                {
                    string sql = "delete from NEWS where newsID="+i;
                    sqlList.Add(sql);
                }
             bool secess=_db.ExcuteSqls(sqlList);


            }
        }
      
    }
}
